package org.jeecg.modules.wlhy.hycustomerbank.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hycustomerbank.entity.HyCustomerBank;
import org.jeecg.modules.wlhy.hycustomerbank.service.IHyCustomerBankService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 货主银行卡信息
 * @Author: duanlikao
 * @Date:   2020-12-15
 * @Version: V1.0
 */
@Api(tags="货主银行卡信息")
@RestController
@RequestMapping("/hycustomerbank/hyCustomerBank")
@Slf4j
public class HyCustomerBankController extends JeecgController<HyCustomerBank, IHyCustomerBankService> {
	@Autowired
	private IHyCustomerBankService hyCustomerBankService;

	 @Autowired
	 private IHyCustomerService hyCustomerService;

	 @Autowired
	 private UserUtil userUtil;


	@ApiOperation(value="货主银行卡信息-分页列表查询", notes="货主银行卡信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCustomerBank hyCustomerBank,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCustomerBank> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerBank, req.getParameterMap());
		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if(customer !=null){
				queryWrapper.eq("customer_id",customer.getId());
			}
		}
		Page<HyCustomerBank> page = new Page<HyCustomerBank>(pageNo, pageSize);
		IPage<HyCustomerBank> pageList = hyCustomerBankService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hyCustomerBank
	 * @return
	 */
	@AutoLog(value = "货主银行卡信息-添加")
	@ApiOperation(value="货主银行卡信息-添加", notes="货主银行卡信息-添加")
	@PostMapping(value = "/add")
	@RequiresPermissions("customer:bank:manager")
	public Result<?> add(@RequestBody HyCustomerBank hyCustomerBank) {
		HyCustomer customer = null;
		if(userUtil.isSys()){
			String customerId = hyCustomerBank.getCustomerId();
			customer = hyCustomerService.getById(customerId);
		}else {
			customer = userUtil.getCustomer();
		}
		if(customer==null){
			return Result.error("货主信息不存在!");
		}
		hyCustomerBank.setBankNum(hyCustomerBank.getBankNum().replaceAll(" ",""));
		hyCustomerBank.setCustomerId(customer.getId());
		hyCustomerBank.setUserId(customer.getUserId());
		hyCustomerBankService.save(hyCustomerBank);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hyCustomerBank
	 * @return
	 */
	@AutoLog(value = "货主银行卡信息-编辑")
	@ApiOperation(value="货主银行卡信息-编辑", notes="货主银行卡信息-编辑")
	@PutMapping(value = "/edit")
	@RequiresPermissions("customer:bank:manager")
	public Result<?> edit(@RequestBody HyCustomerBank hyCustomerBank) {
		HyCustomer customer = null;
		if(userUtil.isSys()){
			String customerId = hyCustomerBank.getCustomerId();
			customer = hyCustomerService.getById(customerId);
			if(customer==null){
				return Result.error("货主信息不存在!");
			}
		}else {
			customer = userUtil.getCustomer();
			if(!StringUtils.equals(customer.getId() , hyCustomerBank.getCustomerId())){
				return Result.error("货主信息错误!");
			}
		}
		hyCustomerBank.setBankNum(hyCustomerBank.getBankNum().replaceAll(" ",""));
		hyCustomerBankService.updateById(hyCustomerBank);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "货主银行卡信息-通过id删除")
	@ApiOperation(value="货主银行卡信息-通过id删除", notes="货主银行卡信息-通过id删除")
	@DeleteMapping(value = "/delete")
	@RequiresPermissions("customer:bank:manager")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		if(!userUtil.isSys()){
			HyCustomerBank customerBank = hyCustomerBankService.getById(id);
			if(customerBank == null){
				return Result.error("银行卡信息不存在!");
			}
			HyCustomer customer = userUtil.getCustomer();
			if(!StringUtils.equals(customer.getId() , customerBank.getCustomerId())){
				return Result.error("货主信息错误!");
			}
		}
		hyCustomerBankService.removeById(id);
		return Result.OK("删除成功!");
	}



	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="货主银行卡信息-通过id查询", notes="货主银行卡信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCustomerBank hyCustomerBank = hyCustomerBankService.getById(id);
		if(hyCustomerBank==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCustomerBank);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyCustomerBank
    */
    @RequestMapping(value = "/exportXls")
	@RequiresPermissions("customer:bank:manager")
    public ModelAndView exportXls(HttpServletRequest request, HyCustomerBank hyCustomerBank) {
        return super.exportXls(request, hyCustomerBank, HyCustomerBank.class, "货主银行卡信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//	 @RequiresPermissions("customer:bank:manager")
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, HyCustomerBank.class);
//    }

}
